package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.Customer;
import com.neuedu.yyzx.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gao
 * 项目：yyzx-project
 * site:http://...
 * 描述：客户入住 前端页面 checkIn 数据库 customer
 * 思路：
 * @data 2025/4/9/10:41
 */
@RestController
@RequestMapping("/checkIn")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping("/page")
    public Ret<List<Customer>> page(Page<Customer> page, Customer customer,String flag) {
        // 创建LambdaQueryWrapper对象，用于后续的条件构造
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();

        // 模糊查询
        // 如果用户昵称不为空，则添加昵称模糊查询条件
        if (customer.getCustomerName() != null) {
            queryWrapper.like(Customer::getCustomerName, customer.getCustomerName());
        }
        if (customer.getIdcard() != null) {
            queryWrapper.like(Customer::getIdcard, customer.getIdcard());
        }
        if (customer.getRoomNo() != null) {
            queryWrapper.like(Customer::getRoomNo, customer.getRoomNo());
        }

        //无管家的客户
        if("1".equals(flag)){
            queryWrapper.isNull(Customer::getUserId);
        }
        // 根据userId查询客户信息
        // long userId = securityUtils.getUserInfo().getId();
        if (customer.getUserId() != null) {
            queryWrapper.eq(Customer::getUserId, customer.getUserId());
        } else {
            // 添加 userId 为 null 的查询条件
//            queryWrapper.isNull(Customer::getUserId);
        }

        // 添加按创建时间降序排序的条件
        queryWrapper.orderByDesc(Customer::getCheckinDate);

        // 调用CustomerService的分页查询方法，并返回查询结果
        return Ret.ok(customerService.page(page, queryWrapper));
    }

    // 保存或更新用户信息
    @PostMapping("/saveOrUpdate")
    // 前段的axios传递的json数据，需要使用@RequestBody注解
    public Ret<Boolean> saveOrUpdate(@RequestBody Customer customer) {
        boolean saveOrUpdate = customerService.saveOrUpdate(customer);
        return Ret.ok(saveOrUpdate);
    }

    // 根据主键查询 前段用于修改信息
    @GetMapping("/get/{id}")
    public Ret<Customer> getById(@PathVariable int id) {
        Customer customer = customerService.getById(id);
        return Ret.ok(customer);
    }

    // 根据主键删除
    @DeleteMapping("/delete/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        return Ret.ok(customerService.removeById(id));
    }

    // http://localhost:8080/checkIn/listAll
    @GetMapping("/listAll")
    public Ret<List<Customer>> listAllCustomers() {
        // 查询未删除的客户(is_deleted = 0)
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        // 只查询未删除的客户
        // queryWrapper.eq(Customer::getIsDeleted, 0);
        List<Customer> customers = customerService.list(queryWrapper);
        return Ret.ok(customers);
    }

    //删除userId的值
    @PostMapping("/updateUserId/{id}")
    public Ret<Customer> updateUserIdById(@PathVariable int id) {
        Customer customer = customerService.getById(id);
        if (customer == null) {
            return Ret.error("客户不存在");
        }
        customer.setUserId(null);
        customerService.updateById(customer);
        return Ret.ok(customer);
    }

    //增加userId的值
    @PostMapping("/addUserId")
    public Ret<Boolean> addUserId(@RequestBody Customer customer) {
        UpdateWrapper<Customer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("customer_id", customer.getCustomerId());
        customer.setUserId(customer.getUserId());
        return customerService.update(customer, updateWrapper) ? Ret.ok(customer) : Ret.error("更新失败");
    }
}
